package com.xuecheng.content.test;

import cn.hutool.core.date.DateTime;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.UUID;

public class FileUploader {

    private String ak = "minio";
    private String sk = "minio123";
    private String endPoint= "http://192.168.136.150:9000";
    private String bucket = "xuecheng";
    private String domain ="http://192.168.136.150:9000";


    @Test
    public void testUpload() throws Exception {

        FileInputStream inputStream = new FileInputStream("E:\\微信图片_20220801101926.jpg");
        String filename = "微信图片_20220801101926.jpg";

        //1、初始化client对象
        MinioClient client = MinioClient
                .builder()
                .endpoint(endPoint)
                .credentials(ak, sk)
                .build();
        
        //2、配置上传的请求地址
        String filePath = "/images/"+
                DateTime.now().toString("yyyy/MM/dd/") +
                UUID.randomUUID().toString() + filename;

        //3、文件上传
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .object(filePath) // 文件名
                .contentType("image/jpg") // 文件类型
                .bucket(bucket) // 桶名
                .stream(inputStream, inputStream.available(), -1)  //参数1:文件流  参数2:文件大小  参数3: 分片大小
                .build();
        client.putObject(putObjectArgs);
        
        //4、获取请求地址
        String urlPath = domain +"/" + bucket + filePath;
        System.out.println(urlPath);
    }
}